// Copyright (c) Huawei Technologies Co., Ltd. 2025. All rights reserved.
// This source file is part of the Cangjie project, licensed under Apache-2.0
// with Runtime Library Exception.
//
// See https://cangjie-lang.cn/pages/LICENSE for license information.

#ifndef CANGJIE_MIGRATOR_DYNAMIC_LIBRARY
#define CANGJIE_MIGRATOR_DYNAMIC_LIBRARY

#include <memory>
#include <string>

#include "Path.h"

namespace Platform {

class DynamicLibraryImpl;

class DynamicLibrary {
public:
    explicit DynamicLibrary(const Path& path);
    ~DynamicLibrary();

    template <typename FuncPointer>
    FuncPointer GetSymbol(const std::string& name)
    {
        return reinterpret_cast<FuncPointer>(GetSymbolRaw(name));
    }

    class OpenFailed final : std::exception {
    public:
        OpenFailed(Path file, std::string errorMessage);
        [[nodiscard]] std::string ToString() const;

        const Path File;
        const std::string ErrorMessage;
    };

private:
    void *GetSymbolRaw(const std::string &name) const;

    std::unique_ptr<DynamicLibraryImpl> impl;
};

} // namespace Platform

#endif // CANGJIE_MIGRATOR_DYNAMIC_LIBRARY
